/*
 * NetReceiver.cpp
 *
 *  Created on: 2011-04-18
 *      Author: jacek
 */

#include "NetUDPReceiver.h"

NetUDPReceiver::NetUDPReceiver() {
	package.CreateReceiverPackage(sizeof(Event));
}
NetUDPReceiver::NetUDPReceiver(Socket &src) {
	receiverSock = src;
	package.CreateReceiverPackage(sizeof(Event));
}
NetUDPReceiver::~NetUDPReceiver() {

}

void NetUDPReceiver::Intialization() {

}

void NetUDPReceiver::ConvertEventNetToHost()
{
	eventBuf->type = ntohl(eventBuf->type);
    eventBuf->destination = ntohl(eventBuf->destination);
    eventBuf->sender = ntohl(eventBuf->sender);

}

void NetUDPReceiver::Updata(Event event) {
	if (event.type == NET_RECV) {
		receiverSock.RecevFrom(&package, &peer);
		eventBuf = reinterpret_cast<Event*> (package.buffor);
		ConvertEventNetToHost();
		SendEvent(*eventBuf);
	}
}

void NetUDPReceiver::SendEvent(Event event) {
	parent->SendEvent(event);
}

void NetUDPReceiver::Dispose() {
	eventBuf=NULL;
}

